package ru.ifmo.swing.genetics.config.fitness;

import org.uncommons.watchmaker.framework.FitnessEvaluator;
import ru.ifmo.swing.EvolutionControlWithConfigProvider;
import ru.ifmo.swing.genetics.config.common.Chooser;
import ru.ifmo.swing.genetics.config.common.ChooserListener;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author avhaliullin
 */

public class FitnessFunctionChooser<S> implements FitnessEvaluator<S>, EvolutionControlWithConfigProvider {
    private final Chooser<FitnessEvaluator<S>> chooser;
    private FitnessEvaluator<S> current;

    public FitnessFunctionChooser(Map<String, FitnessEvaluator<S>> options) {
        this.chooser = new Chooser<FitnessEvaluator<S>>(options);
        current = chooser.getItem();
        chooser.addListener(new ChooserListener<FitnessEvaluator<S>>() {
            public void chosen(FitnessEvaluator<S> fitnessEvaluator) {
                current = fitnessEvaluator;
            }
        });
    }

    public JComponent getControl() {
        return chooser;
    }

    public void reset() {
        chooser.reset();
    }

    public void setDescription(String description) {
        chooser.setTitle(description);
    }

    public double getFitness(S candidate, List<? extends S> population) {
        return current.getFitness(candidate, population);
    }

    public double getFitness(String candidate, List<? extends String> population) {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public boolean isNatural() {
        return current.isNatural();
    }

    public Map<String, Object> getParamValues() {
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("Selected fitness", chooser.getItem());
        return res;
    }
}
